/****************************************************************************
**
** Copyright (C) 2010 Tieto Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Tieto Corporation (info.ostrava@tieto.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Tieto.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Tieto gives you certain additional
** rights.  These rights are described in the Tieto Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Tieto at info.ostrava@tieto.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtGui/QAction>
#include <QtGui/QActionGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QFileDialog>
#include <QtGui/QPushButton>
#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QScrollArea>
#include <QDebug>

#include "convolutionpage.h"
#include "convolutionmodel.h"
#include "convolutionfactory.h"
#include "convolutiontask.h"
#include "convolutionmask.h"
#include "imagemodel.h"
#include "imageview.h"
#include "backgroundtaskwatcher.h"
#include "common.h"

ConvolutionPage::ConvolutionPage(ConvolutionModel &model, QWidget *parent)
    : QWizardPage(parent),
    _model(model)
{
    factory = new ConvolutionFactory(_model);

    setTitle(tr("Image convolution"));
    setSubTitle(tr("Image convolution is used for image processing such special effects, "
                   "filtering, photo adjustment and computer image analysis."));

    createActions();

    sourceView = new ImageView(ConvolutionModel::ESourceImage);
    sourceView->setToolTip(tr("Original image"));
    sourceView->setModel(&_model);

    QPushButton *openButton = new QPushButton;    
    openButton->setText(tr("Open"));
    openButton->setToolTip(tr("Open image"));

    connect(openButton, SIGNAL(clicked()), this, SLOT(openImage()));

    QVBoxLayout *sourceImageLayout = new QVBoxLayout;
    sourceImageLayout->addWidget(sourceView);

    sourceImageLayout->addWidget(openButton);

    convolView = new ImageView(ConvolutionModel::EConvolvedImage);
    convolView->setToolTip(tr("Convolved image"));
    convolView->setModel(&_model);

    QPushButton *convolveButton = new QPushButton;
    convolveButton->setText(tr("Convolve"));
    convolveButton->setToolTip(tr("Convolve image"));

    QObject::connect(convolveButton, SIGNAL(clicked()), this, SLOT(convolveImage()));    

    QVBoxLayout *convolvedImageLayout = new QVBoxLayout;
    convolvedImageLayout->addWidget(convolView);
    convolvedImageLayout->addWidget(convolveButton);

    QScrollArea *scrollArea = new QScrollArea();
    //scrollArea->setWidgetResizable(true);
    QHBoxLayout *convolutionMaskLayout = new QHBoxLayout(scrollArea);
    // TODO QVector<ConvolutionMask*> &masks = _model.getConvolutionMasks();
    /* for (int i = 0; i < masks.size(); i++) {
        ConvolutionMask* mask = masks[i];
        int rows = mask->rows();
        int cols = mask->cols();
        QGridLayout *gridLayout = new QGridLayout();
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                double value = mask->value(row, col);
                QLabel *valueLabel = new QLabel("1"); // TODO                
                valueLabel->setFrameStyle(QFrame::StyledPanel); // TODO
                valueLabel->setMinimumSize(10, 10);
                //valueLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
                gridLayout->addWidget(valueLabel, row, col);
                gridLayout->setColumnMinimumWidth(col, 10);
            }

        }
        convolutionMaskLayout->addLayout(gridLayout);
    }
*/
    QHBoxLayout *imagesLayout = new QHBoxLayout;
    imagesLayout->addLayout(sourceImageLayout);
    imagesLayout->addLayout(convolvedImageLayout);
    imagesLayout->setStretch(0, 1);
    imagesLayout->setStretch(1, 1);

    QVBoxLayout *convolutionLayout = new QVBoxLayout;
    convolutionLayout->addLayout(imagesLayout);
    // TODO convolutionLayout->addLayout(convolutionMaskLayout);
    convolutionLayout->addWidget(scrollArea);
    convolutionLayout->setStretch(0, 2);
    convolutionLayout->setStretch(1, 1);

    QLabel *stepsLabel = new QLabel();
    stepsLabel->setText(tr("1. Open image\n"
                           "2. Select convolution mask\n"
                           "3. Convolve image"));
    stepsLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    stepsLabel->setWordWrap(true);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(stepsLabel);
    layout->addLayout(convolutionLayout);
    layout->setStretch(0, 1);
    layout->setStretch(1, 4);
    this->setLayout(layout);
}

void ConvolutionPage::initializePage()
{
    QImage image;
    if (ImageModel::openImage(":/images/test.bmp", &image)) {
        GrayscaleImage sourceImage;
        sourceImage = ImageModel::convertImage(image).copy(); // MEM
        _model.setSourceImage(sourceImage);
    }
}

void ConvolutionPage::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu(this);
    menu.addAction(openAct);
    menu.addAction(convolveAct);
    menu.addAction(aboutAct);
    menu.exec(event->globalPos());
}

void ConvolutionPage::about()
{
    qDebug() << "ConvolutionPage::about";
    QMessageBox::about(this, tr("Image convolution"),
                       tr("1. Provide image.\n"
                          "2. Select convolution mask\n"
                          "3. Convolve image"));
}

void ConvolutionPage::openImage()
{
    qDebug() << "ConvolutionPage::openImage";
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open source image"));
    if (!fileName.isEmpty()) {
        QImage image;
        if (ImageModel::openImage(fileName, &image)) {
            GrayscaleImage sourceImage;
            sourceImage = ImageModel::convertImage(image).copy(); // MEM
            _model.setSourceImage(sourceImage);

        }
    }
}

void ConvolutionPage::convolveImage()
{
    qDebug() << "ConvolutionPage::convolveImage";
    ConvolutionTask *task = factory->createConvolutionTask(QString("TODO"));
    if (task) {
        BackgroundTaskWatcher watcher(task, tr("Convoluting..."), 0);        
        watcher.exec();        ;
    }
}

void ConvolutionPage::createActions()
{
    openAct = new QAction(tr("&Open"), this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setStatusTip(tr("Open an existing file"));
    QObject::connect(openAct, SIGNAL(triggered()), this, SLOT(openImage()));

    convolveAct = new QAction(tr("&Convolve"), this);
    convolveAct->setStatusTip(tr("Convolve provided image"));
    QObject::connect(convolveAct, SIGNAL(triggered()), this, SLOT(convolveImage()));

    aboutAct = new QAction(tr("&About"), this);
    aboutAct->setStatusTip(tr("Show the info about convolution"));
    QObject::connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}
